#include  "drv_PIM.h"
#include "system.h"
 void drv_HwIOInit( void )
{
    /* Package Code Register
    ** PKGCR2 PKGCR1 PKGCR0 
    **  1      1      0    100 LQFP
    */
  	PKGCR = 0x03;
 

 


/*********************************************************************/   
    /*Port T
    PT7: NC            0
    PT6: NC            0
    PT5: O   CAMR_EN   1
    PT4: O   CAML_EN   1
    PT3: O   VCC5V_PG  1
    PT2: I   BACK_IN   0
    PT1: I   TURN_L    0
    PT0: I   TURN_R    0
    DR:  0000 0000         
    DDR: 0000 0000  
    */
  	PTT	 = 0x00;  /* Port T Data Register */     
        //bit: 76543210
  	DDRT  = (0b00111000);  /* Port T Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */  
                    
    PPST = 0x00;  /* Port T pull device select,Configure pull device polarity on input pin */
                  /* Port T Polarity Select Register, 
                  ** 1:A pull-down device is selected
                  ** 0:A pull-up device is selected
                  */
                   
    PERT = 0x00;  // enable pull device
                  /* Port T Pull Device Enable Register */
                  /* 1:Pull device enabled 
                  ** 0:Pull device disabled
                  */

    /*Port S
    S7: O CAN_STB
    S6: NC 
    S5: NC 
    S4: NC 
    S3: NC
    S2: O LIN1_EN
    S1: O LIN1_OUT
    S0: I LIN1_IN
    DR:   
    DDR: 
    */             
  	PTS = 0x00;  /* Port S Data Register */  
        //bit: 76543210
  	DDRS  = (0b10000110);  /* Port S Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */  
                                  
  	PPSS = 0x80;  /* Port S Polarity Select Register */ 
  	              /* Port S pull device select.Configure pull device polarity on input pin
                  ** This bit selects a pullup or a pulldown device if enabled on the associated
                  ** port input pin.
                  ** 1 Pulldown device selected
                  ** 0 Pullup device selected
                  */
                  	
  	PERS = 0x80;  /* Port S pull device enable  */
  	              /* Enable pull devices on input pins
  	              ** 1 Pull device enabled.
                  ** 0 Pull device disabled.
  	              */
  	              	
    WOMS = 0x00;  /* Port S Wired-Or Mode Register */
                  /* This register configures the output pins as wired-or. */
                  /* 1 Output buffers operate as open-drain outputs.
                  ** 0 Output buffers operate as push-pull outputs.
                  */                       
/*********************************************************************/  
    /*Port M
    M7:   
    M6:   
    M5:   
    M4: 
    M3: NC 
    M2: NC 
    M1: O CAN_TX
    M0: I CAN_RX
    DR:   0000 0000
    DDR:  0010
    */             
  	PTM = 0x00;  /* Port M Data Register */  
        //bit: 76543210
  	DDRM  = (0b0010);  /* Port M Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */  
                                
    PPSM = 0x00; /* Port M Polarity Select Register */  
                  /* 1 Pulldown device selected
                  ** 0 Pullup device selected
                  */
                      
    PERM = 0x00; /* Port M Pull Device Enable Register */
  	              /* Enable pull devices on input pins
  	              ** 1 Pull device enabled.
                  ** 0 Pull device disabled.
  	              */    
    
    WOMM = 0x00; /* Port M Wired-Or Mode Register */
                 /* 1 Output buffer operates as open-drain output.
                 ** 0 Output buffer operates as push-pull output.    
                 */                					 			
/*********************************************************************/  
    /*Port P
    P7: NC           0
    P6: NC           0
    P5: O  CAMB_EN   1
    P4: O  CAMF_EN   1
    P3: I  IR_RX     0
    P2: NC           0
    P1: I AVM_ONOFF  0
    P0: I ACC_IN     0
    DR:   0000 0000
    DDR:  0000 0000

    */ 
  	PTP   = 0x00;  /* Port P Data Register */
        //bit: 76543210
  	DDRP  = (0b00110000);  /* Port P Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */
    PERP = 0x00;  /* Port P Pull Device Enable Register */
  	              /* 1 Pull device enabled
                  ** 0 Pull device disabled
                  */
                   
    PPSP = 0x00;  /* Port P pull device select */
                  /* 1 Pulldown device selected; rising edge selected
                  ** 0 Pullup device selected; falling edge selected
                  */

    /*Port J
    J7: NC 
    J6: NC 
    J5: NC 
    J4: NC 
    J3: NC 
    J2: NC 
    J1: NC 
    J0: NC 
    DR:   0000 0000
    DDR: 
    */             
  	PTJ = 0x00;  /* Port J Data Register */  
        //bit: 76543210
  	DDRJ  = (0b00000000);  /* Port J Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */  
                  
    PTIJ = 0x00;  /* Port J Input Register */
    PERJ = 0x00;  /* Port J Pull Device Enable Register */
  	              /* 1 Pull device enabled
                  ** 0 Pull device disabled
                  */
                   
    PPSJ = 0x00;  /* Port J pull device select */
                  /* 1 Pulldown device selected; rising edge selected
                  ** 0 Pullup device selected; falling edge selected
                  */
                  
    PIEJ = 0x00;  /* Port J interrupt flag */
                  /* 1 Interrupt is enabled
                  ** 0 Interrupt is disabled (interrupt flag masked)
                  */
                  
      PIFJ = 0x00;  /* Port J Interrupt Flag Register */

    /**********************************************************
   *	PORT AD SETUP
   *
   *	configure port as input w/ pull-ups enabled,  
   *	key-wakeup enabled on PT1AD[7:4] for PBSW use
   *    NOTE: to use the digital input function the ATDDIEN bit must be set  
   *
   *	NOTE: ATD port pull-ups are active for both digital and 
   *	analog inputs !  
   ***********************************************************/
    /*Port PT1AD(PADL)
    Important!!
    DDR1AD related to PAD0-7, 
    DDR0AD related to PAD8-15
    
    AD7: O    VCC5V_EN           1
    AD6: O    MCU_KEEP           1 
    AD5: I    GND                0
    AD4: O    CAN_ERR_EN         1
    AD3: O    CAN_MODE_EN        1
    AD2: AIN  CAMB_V             0
    AD1: AIN  CAML_V             0
    AD0: NC                      0
    DR:   
    DDR: 
    */ 
  	PT1AD = 0x00;  /* Port AD Data Register */  
          //bit: 76543210
  	DDR1AD  = (0b11011000);  /* Port AD Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */  
                    
    PPS1AD = 0x00; /* Port AD Polarity Select Register */ 
                   /* 1 Pulldown device selected; rising edge selected
                   ** 0 Pullup device selected; falling edge selected
                   */
                        
    PER1AD = 0x00; /* Port AD Pull Enable Register */ 
                   /* Enable pull device on input pin
	               ** 1 Pull device enabled
                   ** 0 Pull device disabled
	               */ 
	               	
    PIE1AD = 0x00; /* Port AD Interrupt Enable Register */        	
                   /* 1:Interrupt is enabled.
                   ** 0:Interrupt is disabled (interrupt flag masked).
                   */ 
 //   ATDDIEN_IEN = (0b00101000);  /* 0 Disable digital input buffer to ANx pin */              
    /*Port PT0AD(PADH)
    Important!!
    DDR1AD related to PAD0-7, 
    DDR0AD related to PAD8-15
   
    AD15: NC                        0
    AD14: NC                        0 
    AD13: NC                        0
    AD12: NC                        0
    AD11: O   CAN_MODE_STB_N        0
    AD10: AN  CAMR_V                0
    AD9 : AN  CAMB_V                0
    AD8 : AN  VBAT_ADC              0
    DR:   0000 0000
    DDR: 0000 0000
    */             
  	PT0AD = 0x00;  /* Port AD Data Register */  
          //bit: 76543210
  	DDR0AD  = (0b00001000);  /* Port AD Data Direction Register */    	
                   /* 1:Associated pin is configured as output
                   ** 0:Associated pin is configured as input
                   */  
                    
    PPS0AD = (0b00000000); /* Port AD Polarity Select Register */ 
                   /* 1 Pulldown device selected; rising edge selected
                   ** 0 Pullup device selected; falling edge selected
                   */
                        
    PER0AD = 0x00; /* Port AD Pull Enable Register */ 
                   /* Enable pull device on input pin
	               ** 1 Pull device enabled
                   ** 0 Pull device disabled
	               */
	                	
    PIE0AD = 0x00; /* Port AD Interrupt Enable Register */        	
	               /* 1:Interrupt is enabled.
                   ** 0:Interrupt is disabled (interrupt flag masked).
                   */  

     /* ATD Input Enable Register:
     ** 0 Disable digital input buffer to ANx pin
     ** 1 Enable digital input buffer on ANx pin.
     **
     */    //        7654321076543210
     ATDDIEN_IEN = 0b0000000011011000; 
    //      ACMPC_ACDIEN = 0x0f;   
}